package com.aplink.generic.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.aplink.sg.BuildConfig;

public class LogUtil {
	public static final String RESOURCE_HELPER = "ResourceHelper";
	public static final String TAG_AUTO_SYNC = "APLINK_AUTO_SYNC";
	public static final String TAG_DATABASE = "APLINK_DATABASE";
	public static final String TAG_DISK_CACHE = "APLINK_DISK_CACHE";
	public static final String TAG_ERROR = "APLINK_ERROR";
	public static final String TAG_INFO = "APLINK_INFO";
	public static final String TAG_JSON_VALUE = "APLINK_JSON_VALUE";
	public static final String TAG_LOCATION = "APLINK_LOCATION";
	public static final String TAG_WEBSERVICE = "APLINK_WEBSERVICE";

	public static void error(final String message, final String action,
	        Class<?> className) {
		if (BuildConfig.DEBUG) {
			Log.e(LogUtil.TAG_ERROR,
			        format(message, action, className.getSimpleName()));
		}
	}

	public static void error(final String message, final String action,
	        String className) {
		if (BuildConfig.DEBUG) {
			Log.e(LogUtil.TAG_ERROR, format(message, action, className));
		}
	}

	public static void error(final String errorTag, final String message,
	        final String action, Class<?> className) {
		if (BuildConfig.DEBUG) {
			Log.e(errorTag, format(message, action, className.getSimpleName()));
		}
	}

	public static void error(final String errorTag, final String message,
	        final String action, String className) {
		if (BuildConfig.DEBUG) {
			Log.e(errorTag, format(message, action, className));
		}
	}

	private static String format(final String message, final String action,
	        final String className) {
		final SimpleDateFormat sdf = new SimpleDateFormat(
		        "dd.MM.yyyy HH:mm:ss", Locale.getDefault());
		final String currentDateandTime = sdf.format(new Date());

		try {
			final JSONObject jsonObject = new JSONObject();
			jsonObject.put("Class:", className);
			jsonObject.put("Action:", action);
			jsonObject.put("Datetime:", currentDateandTime);
			jsonObject.put("Message:", message);
			return jsonObject.toString(4).replace("\\", "");
		} catch (final JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void info(final String message, final String action,
	        final Class<?> className) {
		if (BuildConfig.DEBUG) {
			Log.i(LogUtil.TAG_INFO,
			        format(message, action, className.getSimpleName()));
		}
	}

	public static void info(final String message, final String action,
	        final String className) {
		if (BuildConfig.DEBUG) {
			Log.i(LogUtil.TAG_INFO, format(message, action, className));
		}
	}

	public static void info(final String infoTag, final String message,
	        final String action, Class<?> className) {
		if (BuildConfig.DEBUG) {
			Log.i(infoTag, format(message, action, className.getSimpleName()));
		}
	}

	public static void info(final String infoTag, final String message,
	        final String action, final String className) {
		if (BuildConfig.DEBUG) {
			Log.i(infoTag, format(message, action, className));
		}
	}
}
